﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Simhopp.Model;
using Simhopp.Presenter;

namespace Simhopp.GUI
{
    //Lägg till knapp för att ta bort en DIVE från DIVE list
    public partial class frmEditDivers : Form, InterfaceEditDiverView
    {

        public event DelegateAddDiver EventAddDiver = null;
        public event DelegateRemoveDiver EventRemoveDiver = null;
        public event DelegateUpdateDiver EventUpdateDiver = null;
        public event DelegateAddDive EventAddDive = null;
        public event DelegateRemoveDive EventRemoveDive = null;
        public event DelegateGetDiverList EventGetDiverList = null;
        public event DelegateGetDiveList EventGetDiveList = null;
        public event DelegateGetAllDivesFromDatabase EventGetAllDivesFromDatabase = null;
        PresenterEditDiver pEditDiver;
        public event DelegateUpdateDiverList EventUpdateDiverList = null;
        public event DelegateAddExistingDiver EventAddExistingDiver = null;
        public event DelegateUpdateCompleteDiverList EventUpdateCompleteDiverList = null;

        public frmEditDivers()
        {
            InitializeComponent();
            cmbNationality.Items.AddRange(Enum.GetNames(typeof(Simhopp.Model.Countries)));
        }


        public frmEditDivers(Competition PropCompetition)
        {
            InitializeComponent();
            cmbNationality.Items.AddRange(Enum.GetNames(typeof(Simhopp.Model.Countries)));
            pEditDiver = new PresenterEditDiver(this ,PropCompetition);
            lblCompetition.Text = PropCompetition.Name.ToString();
        }
        //Add dive button
        private void button1_Click(object sender, EventArgs e)
        {
            if (pEditDiver._modelDiver != null && EventAddDive != null)
            {
                Dive dive = (Dive)cmbDives.SelectedItem;
                dive.DiveNumber = Convert.ToInt32(cmbDiveOrder.SelectedItem);
                EventAddDive(dive);
                UpdateDiveList();
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (EventAddDiver != null)
            {
                EventAddDiver(new Diver(0,txtName.Text,dateBirthday.Value,CheckGenderRadioButton(rdoFemale,rdoMale),
                    Convert.ToInt32(txtStartNumber.Text), 0, (Countries)Enum.Parse(typeof(Countries), cmbNationality.SelectedItem.ToString())));
                EmptyInformation();
                UpdateDiverListNew();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (EventUpdateDiver != null && lstvwDivers.SelectedItems.Count == 1)
            {
                EventUpdateDiver((Diver)lstvwDivers.SelectedItems[0].Tag, new Diver(0, txtName.Text, dateBirthday.Value, CheckGenderRadioButton(rdoFemale, rdoMale),
                    Convert.ToInt32(txtStartNumber.Text), 0, (Countries)Enum.Parse(typeof(Countries), cmbNationality.SelectedItem.ToString())));
                EmptyInformation();
                UpdateDiverListNew();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (EventRemoveDiver != null && lstvwDivers.SelectedItems.Count == 1)
            {
                EventRemoveDiver((Diver)lstvwDivers.SelectedItems[0].Tag);
                EmptyInformation();
                UpdateDiverListNew();
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lstvwDivers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstvwDivers.SelectedItems.Count == 1)
            {
                FillInformation((Diver)lstvwDivers.SelectedItems[0].Tag);
                pEditDiver._modelDiver = (Diver)lstvwDivers.SelectedItems[0].Tag;
                UpdateDiveList();
            }
        }
        private void UpdateDiverList()
        {
            lstvwDivers.Items.Clear();
            if (EventGetDiverList != null)
            {
                foreach (Diver diver in EventGetDiverList())
                {
                    ListViewItem tempListView = new ListViewItem(diver.StartNumber.ToString());
                    tempListView.SubItems.Add(diver.Name);
                    tempListView.SubItems.Add(diver.Gender.ToString());
                    tempListView.SubItems.Add(diver.DateOfBirth.ToShortDateString());
                    tempListView.SubItems.Add(diver.Nationality.ToString());
                    tempListView.Tag = diver;
                    lstvwDivers.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwDivers.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }

        private void UpdateDiveList()
        {
            
            lstvwDives.Items.Clear();
            List<Dive> diveList;
            if (EventGetDiveList != null)
            {
                diveList = EventGetDiveList();
                diveList.Sort((x, y) => x.DiveNumber.CompareTo(y.DiveNumber));
                foreach (Dive dive in diveList)
                {
                    ListViewItem tempListView = new ListViewItem(dive.DiveNumber.ToString());
                    tempListView.SubItems.Add(dive.Name);
                    tempListView.Tag = dive;
                    lstvwDives.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwDives.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }
        private void FillInformation(Diver diver)
        {
            txtStartNumber.Text = diver.StartNumber.ToString();
            txtName.Text = diver.Name;
            if (diver.Gender == 'F')
            {
                rdoFemale.Checked = true;
                rdoMale.Checked = false;
            }
            if (diver.Gender == 'M')
            {
                rdoMale.Checked = true;
                rdoFemale.Checked = false;
            }
            dateBirthday.Value = diver.DateOfBirth;
            cmbNationality.SelectedIndex = (int)diver.Nationality;
        }
        private char CheckGenderRadioButton(RadioButton Female, RadioButton Male)
        {
            if (Female.Checked && !Male.Checked)
                return 'F';
            else 
                return 'M';
        }

        private void rdoFemale_CheckedChanged(object sender, EventArgs e)
        {
            if(rdoFemale.Checked)
                rdoMale.Checked = false;
        }

        private void rdoMale_CheckedChanged(object sender, EventArgs e)
        {
            if(rdoMale.Checked)
                rdoFemale.Checked = false;
        }

        private void frmEditDivers_Load(object sender, EventArgs e)
        {
            UpdateDiverListNew();
            if (EventGetAllDivesFromDatabase != null)
            {
                cmbDives.DataSource = EventGetAllDivesFromDatabase();
                cmbDives.DisplayMember = "Name";
            }
            UpdateCompleteDiverList();
        }
        private void EmptyInformation()
        {
            txtStartNumber.Clear(); txtName.Clear(); dateBirthday.Value = DateTime.Now;
            cmbNationality.SelectedIndex = 0;
        }
        private void UpdateDiverListNew()
        {
            lstvwDivers.Items.Clear();
            List<Diver> diverList;
            if (EventUpdateDiver != null)
            {
                diverList = EventUpdateDiverList();
                diverList.Sort((x, y) => x.StartNumber.CompareTo(y.StartNumber));
                foreach (Diver d in diverList)
                {
                    ListViewItem tempListView = new ListViewItem();
                    tempListView.Text = d.Name;
                    tempListView.SubItems.Add(d.Gender.ToString());
                    tempListView.SubItems.Add(d.DateOfBirth.ToShortDateString());
                    tempListView.SubItems.Add(d.Nationality.ToString());
                    tempListView.SubItems.Add(d.StartNumber.ToString());
                    tempListView.Tag = d;
                    lstvwDivers.Items.Add(tempListView);
                    
                }

            }
            foreach (ListViewItem item in lstvwDivers.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }

        private void btnRemoveDive_Click(object sender, EventArgs e)
        {
            if(EventRemoveDive != null && lstvwDives.SelectedItems.Count == 1)
            {
                EventRemoveDive((Dive)lstvwDives.SelectedItems[0].Tag);
                UpdateDiveList();
            }
        }
        private void UpdateCompleteDiverList()
        {
            lstvwCompleteDiverList.Items.Clear();
                List<Diver> diverList = new List<Diver>();
            if(EventUpdateCompleteDiverList != null)
            {
                EventUpdateCompleteDiverList(diverList);
                foreach(Diver d in diverList)
                {
                    ListViewItem tempListView = new ListViewItem();
                    tempListView.Text = d.Name;
                    tempListView.SubItems.Add(d.Gender.ToString());
                    tempListView.SubItems.Add(d.DateOfBirth.ToShortDateString());
                    tempListView.SubItems.Add(d.Nationality.ToString());
                    tempListView.Tag = d;
                    lstvwCompleteDiverList.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwCompleteDiverList.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Diver diver = lstvwCompleteDiverList.SelectedItems[0].Tag as Diver;
            EventAddExistingDiver(diver);
            UpdateDiverListNew();
        }

        private void grpDives_Enter(object sender, EventArgs e)
        {

        }
    }
}
